/**
 * 
 */
package edu.lnu.project.timeeditapp.parser;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@SuppressWarnings("deprecation")
public class Event {

	private int _id;
	private String	weekday;
	private String 	beginDate;
	private String	beginTime;
	private String 	endDate;
	private String	endTime;
	private	String	course;
	private String	programme;
	private	String	courseEvt;
	private String	programmeEvt;
	private	String	room;
	private String	teacher;
	private String	studentGroup;
	private String	activity;
	private String	examCode;
	private String	comments;
	private String	city;
	private int reminder;
	private int week;

	private int reminderHour;
	private int reminderMinute;
	private int sentinel = -1; //sentinel is a variable to check if reminder is changed.

	/**
	 * @return the weekday
	 */
	public String getWeekday() {
		return weekday;
	}
	/**
	 * @param weekday the weekday to set
	 */
	public void setWeekday(String weekday) {
		this.weekday = weekday;
	}
	/**
	 * @return the beginDate
	 */
	public String getBeginDate() {
		return beginDate;
	}
	/**
	 * @param beginDate the beginDate to set
	 */
	public void setBeginDate(String beginDate) {
		this.beginDate = beginDate;
	}
	/**
	 * @return the beginTime
	 */
	public String getBeginTime() {
		return beginTime;
	}
	/**
	 * @param beginTime the beginTime to set
	 */
	public void setBeginTime(String beginTime) {
		this.beginTime = beginTime;
	}
	/**
	 * @return the endDate
	 */
	public String getEndDate() {
		return endDate;
	}
	/**
	 * @param endDate the endDate to set
	 */
	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}
	/**
	 * @return the endTime
	 */
	public String getEndTime() {
		return endTime;
	}
	/**
	 * @param endTime the endTime to set
	 */
	public void setEndTime(String endTime) {
		this.endTime = endTime;
	}
	/**
	 * @return the course
	 */
	public String getCourse() {
		return course;
	}
	/**
	 * @param course the course to set
	 */
	public void setCourse(String course) {
		this.course = course;
	}
	/**
	 * @return the programme
	 */
	public String getProgramme() {
		return programme;
	}
	/**
	 * @param programme the programme to set
	 */
	public void setProgramme(String programme) {
		this.programme = programme;
	}
	/**
	 * @return the courseEvt
	 */
	public String getCourseEvt() {
		return courseEvt;
	}
	/**
	 * @param courseEvt the courseEvt to set
	 */
	public void setCourseEvt(String courseEvt) {
		this.courseEvt = courseEvt;
	}
	/**
	 * @return the programmeEvt
	 */
	public String getProgrammeEvt() {
		return programmeEvt;
	}
	/**
	 * @param programmeEvt the programmeEvt to set
	 */
	public void setProgrammeEvt(String programmeEvt) {
		this.programmeEvt = programmeEvt;
	}
	/**
	 * @return the room
	 */
	public String getRoom() {
		return room;
	}
	/**
	 * @param room the room to set
	 */
	public void setRoom(String room) {
		this.room = room;
	}
	/**
	 * @return the teacher
	 */
	public String getTeacher() {
		return teacher;
	}
	/**
	 * @param teacher the teacher to set
	 */
	public void setTeacher(String teacher) {
		this.teacher = teacher;
	}
	/**
	 * @return the studentGroup
	 */
	public String getStudentGroup() {
		return studentGroup;
	}
	/**
	 * @param studentGroup the studentGroup to set
	 */
	public void setStudentGroup(String studentGroup) {
		this.studentGroup = studentGroup;
	}
	/**
	 * @return the activity
	 */
	public String getActivity() {
		return activity;
	}
	/**
	 * @param activity the activity to set
	 */
	public void setActivity(String activity) {
		this.activity = activity;
	}
	/**
	 * @return the examCode
	 */
	public String getExamCode() {
		return examCode;
	}
	/**
	 * @param examCode the examCode to set
	 */
	public void setExamCode(String examCode) {
		this.examCode = examCode;
	}
	/**
	 * @return the comments
	 */
	public String getComments() {
		return comments;
	}
	/**
	 * @param comments the comments to set
	 */
	public void setComments(String comments) {
		this.comments = comments;
	}
	/**
	 * @return the city
	 */
	public String getCity() {
		return city;
	}
	/**
	 * @param city the city to set
	 */
	public void setCity(String city) {
		this.city = city;
	}

	/**
	 * @return the reminder status on/off
	 */
	public int getReminder() {
		return reminder;
	}
	/**
	 * @param set the reminder on or off
	 */
	public void setReminder(int reminder) {
		this.reminder = reminder;
	}

	public int getId() {
		return _id;
	}
	public void setId(int _id) {
		this._id = _id;
	}


	/**
	 * Only for debugging
	 */
	@Override
	public String toString(){
		String string = "";

		string = "************** One Event **************"+"\n"+ 
				"Weekday: "+weekday+"*\n*"+
				"Data: "+beginDate+"-"+endDate+"*\n*"+
				"Time: "+beginTime+"-"+endTime+"*\n*"+
				"Course/Programme: "+course+"*\n*"+
				"Course evt/Programme evt: "+courseEvt+"*\n*"+
				"Room: "+room+"*\n*"+
				"Teacher: "+teacher+"*\n*"+
				"StudentGroup: "+studentGroup+"*\n*"+
				"Activity: "+activity+"*\n*"+
				"ExamCode: "+examCode+"*\n*"+
				"Comments: "+comments+"*\n*"+
				"City: "+city+"*\n*"+
				"Week: "+week+"*\n*"+
				"**************************************";

		return string;

	}
	public int getWeek() {
		return week;
	}
	public void setWeek(int week) {
		this.week = week;
	}

	/**
	 * Gets the reminder hour. For example: if reminder is set to 60mins and the event is at 14:00,
	 * it will return 13 as the reminder hour.
	 */
	public int getReminderHour() {
		//Check if sentinel is changed otherwise it won't do the calculations below and CPU is saved from extra calculations.
		if(sentinel == -1 || sentinel != reminder){
			sentinel = reminder;


			int eventHour;
			int eventMinute;
			int temp;

			SimpleDateFormat hourFormat = new SimpleDateFormat("HH:mm");
			Date time = null;
			try {
				time = hourFormat.parse(beginTime);
			} catch (ParseException e) {
				e.printStackTrace();
			}

			eventHour = time.getHours();
			eventMinute = time.getMinutes();

			switch (reminder){
			case (60) : {
				if(eventHour!=0){
					reminderHour = eventHour - 1;
					reminderMinute = eventMinute;
				}
				else{
					reminderHour = 23;
					reminderMinute = eventMinute;
				}
				break;
			}
			case (30) : {
				if(eventMinute > 30){
					reminderMinute = eventMinute - 30;
					reminderHour = eventHour;
				}
				else if (eventMinute < 30){
					temp = 30-eventMinute;
					reminderMinute = 60 - temp;
					reminderHour = eventHour - 1;
				}
				else if(eventMinute == 30){
					reminderMinute = 0;
				}
				break;
			}
			case (15) : {
				if(eventMinute > 15){
					reminderMinute = eventMinute - 15;
					reminderHour = eventHour;
				}
				else if (eventMinute < 15){
					temp = 15 - eventMinute;
					reminderMinute = 60 - temp;
					reminderHour = eventHour - 1;
				}
				else if(eventMinute == 15){
					reminderHour = eventHour;
					reminderMinute = 0;
				}
				break;
			}
			case (5) : {
				if(eventMinute > 5){
					reminderMinute = eventMinute - 5;
					reminderHour = eventHour;
				}
				else if (eventMinute < 5){
					temp = 5 - eventMinute;
					reminderMinute = 60 - temp;
					reminderHour = eventHour - 1;
				}
				else if(eventMinute == 5){
					reminderHour = eventHour;
					reminderMinute = 0;
				}
				break;
			}
			default: break;
			}
		}


		return reminderHour;
	}

	public int getReminderMinute() {
		//Check if sentinel is changed otherwise it won't do the calculations below and CPU is saved from extra calculations.
		if(sentinel == -1 || sentinel != reminder){
			sentinel = reminder;

			int eventHour;
			int eventMinute;
			int temp;

			SimpleDateFormat hourFormat = new SimpleDateFormat("HH:mm");
			Date time = null;
			try {
				time = hourFormat.parse(beginTime);
			} catch (ParseException e) {
				e.printStackTrace();
			}

			eventHour = time.getHours();
			eventMinute = time.getMinutes();

			switch (reminder){
			case (60) : {
				if(eventHour!=0){
					reminderHour = eventHour - 1;
					reminderMinute = eventMinute;
				}
				else{
					reminderHour = 23;
					reminderMinute = eventMinute;
				}
				break;
			}
			case (30) : {
				if(eventMinute > 30){
					reminderMinute = eventMinute - 30;
					reminderHour = eventHour;
				}
				else if (eventMinute < 30){
					temp = 30-eventMinute;
					reminderMinute = 60 - temp;
					reminderHour = eventHour - 1;
				}
				else if(eventMinute == 30){
					reminderHour = eventHour;
					reminderMinute = 0;
				}
				break;
			}
			case (15) : {
				if(eventMinute > 15){
					reminderMinute = eventMinute - 15;
					reminderHour = eventHour;
				}
				else if (eventMinute < 15){
					temp = 15 - eventMinute;
					reminderMinute = 60 - temp;
					reminderHour = eventHour - 1;
				}
				else if(eventMinute == 15){
					reminderHour = eventHour;
					reminderMinute = 0;
				}
				break;
			}
			case (5) : {
				if(eventMinute > 5){
					reminderMinute = eventMinute - 5;
					reminderHour = eventHour;
				}
				else if (eventMinute < 5){
					temp = 5 - eventMinute;
					reminderMinute = 60 - temp;
					reminderHour = eventHour - 1;
				}
				else if(eventMinute == 5){
					reminderHour = eventHour;
					reminderMinute = 0;
				}
				break;
			}
			default: break;
			}
		}

		return reminderMinute;
	}



}
